package com.zh.kotlin_mvvm

import android.util.Log
import com.alibaba.android.arouter.launcher.ARouter
import com.liulishuo.filedownloader.FileDownloader
import com.liulishuo.filedownloader.connection.FileDownloadUrlConnection
import com.p2p.P2PClass
import com.tencent.smtt.export.external.TbsCoreSettings
import com.tencent.smtt.sdk.QbSdk
import com.vanniktech.emoji.EmojiManager
import com.vanniktech.emoji.ios.IosEmojiProvider
import com.zh.common.base.BaseApplication
import com.zh.config.CommonService
import com.zh.config.ZjConfig
import com.zh.kotlin_mvvm.ftp.bean.LocalVideoInfo
import com.zh.common.utils.TtsUtils
import io.microshow.rxffmpeg.RxFFmpegInvoke
import java.util.Vector

class MyApp : BaseApplication() {

    companion object {
        @JvmStatic
        lateinit var instance: MyApp
            private set
    }

    override fun onCreateMethod() {
        instance = this
        initX5
        TtsUtils.instance.init(this)
        RxFFmpegInvoke.getInstance().setDebug(true)
        val commonService =
            ARouter.getInstance().build(ZjConfig.InitMyService).navigation() as CommonService
        commonService.showLog()

        EmojiManager.install(IosEmojiProvider())
        initFileDownloader()
    }

    @Volatile
    var p: P2PClass? = null
    var downTaskPositionList: Vector<LocalVideoInfo> = Vector<LocalVideoInfo>() //后台下载任务的位置
    var downTaskPositionList3: Vector<LocalVideoInfo> = Vector<LocalVideoInfo>() //后台下载任务的位置
    var tid = 0 //当前正在播放电影的tid

    fun getp2p(): P2PClass? {
        if (p == null) {
            p = P2PClass()
        }
        return p
    }

    private val initX5: Unit
        get() {
            // 在调用TBS初始化、创建WebView之前进行如下配置
            val map = HashMap<String, Any>()
            map[TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER] = true
            map[TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE] = true
            QbSdk.initTbsSettings(map)
            //搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
            QbSdk.setDownloadWithoutWifi(true)
            val cb: QbSdk.PreInitCallback = object : QbSdk.PreInitCallback {
                override fun onViewInitFinished(arg0: Boolean) {
                    // TODO Auto-generated method stub
                    //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                    Log.d("QbSdk", " x5 onViewInitFinished is $arg0")
                }

                override fun onCoreInitFinished() {
                    Log.d(
                        "QbSdk",
                        " x5 内核加载完成 版本号:" + QbSdk.getTbsVersion(applicationContext)
                    )
                }
            }
            //x5内核初始化接口
            QbSdk.initX5Environment(this, cb)
        }

    private fun initFileDownloader() {
        FileDownloader.setupOnApplicationOnCreate(this)
            .connectionCreator(
                FileDownloadUrlConnection.Creator(
                    FileDownloadUrlConnection.Configuration()
                        .connectTimeout(15000) // set connection timeout.
                        .readTimeout(15000) // set read timeout.
                )
            )
            .commit()
    }
}